﻿using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace collabr8
{
    public partial class ClientClipboardForm : Form
    {
        StringCollection fileNames;
        string destFolder;
        ClientForm father;
        FileInfo info;

        public ClientClipboardForm(ClientForm father)
        {
            InitializeComponent();
            this.father = father;
            this.Location = father.Location;
            this.Left += 100;
            this.Top += 100;
            this.destFolder = father.UserName + "_clips\\";
            this.fileNames = new StringCollection();
            if (Directory.Exists(father.UserName + "_clips"))
                Directory.Delete(father.UserName + "_clips", true);
            Directory.CreateDirectory(father.UserName + "_clips");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Clipboard.ContainsFileDropList())
            {
                this.fileNames = Clipboard.GetFileDropList();
                this.AddClips();
            }
        }

        private void ClipboardForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
            {
                e.Effect = DragDropEffects.All;
            }
        }

        private void AddClips()
        {
            foreach (string fname in fileNames)
            {
                this.info = new FileInfo(fname);
                if (this.info.Length < 1048576)         // if filesize < 1 MB
                {
                    string[] tok = fname.Split('\\');
                    string name = tok[tok.Length - 1];

                    if (File.Exists(destFolder + name))
                    {
                        int i = 1;
                        while (File.Exists(destFolder + i.ToString() + "-" + name))
                        {
                            i++;
                        }
                        name = i.ToString() + "-" + name;
                    }

                    File.Copy(fname, destFolder + name);
                    this.listBox1.Items.Add(name);
                    this.father.Invoke(father.delegateSendClip, new object[] { name });
                }
                
            }
        }   

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            this.fileNames.Clear();
            this.fileNames.AddRange((string[])e.Data.GetData(DataFormats.FileDrop));
            if (fileNames.Count != 0)
            {
                this.AddClips();
            }
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                System.Diagnostics.Process.Start(destFolder + listBox1.Items[listBox1.SelectedIndex]);
            }
        }

        private void ClipboardForm_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                string[] filePaths = Directory.GetFiles(this.destFolder);
                foreach (string fname in filePaths)
                {
                    string[] tok = fname.Split('\\');
                    string name = tok[tok.Length - 1];
                    if(!listBox1.Items.Contains(name) && name != "desktop.ini")
                        listBox1.Items.Add(name);
                }
            }
        }

        public void AddItem(string item)
        {
            if(!listBox1.Items.Contains(item))
                listBox1.Items.Add(item);
        }

    }
}
